package org.javaforever.cookieshop.controller;

import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.javaforever.cookieshop.domain.PageBean;
import org.javaforever.cookieshop.domain.UserBean;
import org.javaforever.cookieshop.service.CustOrderService;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class AdminUserCustController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected CustOrderService orderService;
	@Autowired
	protected TypeService typeService;
	@Autowired
	protected UserService userService;
    
    @GetMapping("/admin/user_list")
    public String adminUserList(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, Model model) throws Exception{   
	    if(pageNumber<=0)pageNumber=1;
	    PageBean p = userService.getUserPage(pageNumber);
	    if(p.getTotalPage()==0)
	    {
	        p.setTotalPage(1);
	        p.setPageNumber(1);
	    }
	    else {
	        if(pageNumber>=p.getTotalPage()+1)
	        {
	            p = userService.getUserPage(pageNumber);
	        }
	    }
	    model.addAttribute("p", p);
	    return "/admin/user_list";
    }
    
    @GetMapping("/admin/user_editshow")
    public String adminGoodsEditShow(@RequestParam(value = "id", required = true) Long id,Model model) throws Exception{   
        UserBean u = userService.findUserById(id);
        model.addAttribute("u", u);
        return "admin/user_edit";
	}
    
    @GetMapping("/admin/user_delete")
    public String adminUserDelete(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    try {
	        userService.deleteUser(id);
	        return "/admin/user_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "/admin/user_list";
	    }
	}
    
    @GetMapping("/admin/user_softdelete")
    public String adminUserSoftDelete(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    try {
	        userService.softDeleteUser(id);
	        return "/admin/user_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "/admin/user_list";
	    }
	}
    
    @GetMapping("/admin/go_user_add")
    public String adminGoUserAdd(Model model) throws Exception{   
        return "/admin/user_add";
	}
    
    @GetMapping("/admin/go_user_reset")
    public String adminUserReset(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
    	UserBean user = userService.findUserById(id);
    	model.addAttribute("u", user);
    	return "/admin/user_reset";
	}
}
